#! /bin/sh
# $OpenLDAP$
## This work is part of OpenLDAP Software <http://www.openldap.org/>.
##
## Copyright 1998-2022 The OpenLDAP Foundation.
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted only as authorized by the OpenLDAP
## Public License.
##
## A copy of this license is available in the file LICENSE in the
## top-level directory of the distribution or, alternatively, at
## <http://www.OpenLDAP.org/license.html>.

echo "running defines.sh"
. $SRCDIR/scripts/defines.sh

if test $ACCESSLOG = accesslogno; then
	echo "Accesslog overlay not available, test skipped"
	exit 0
fi

XDIR=$TESTDIR/srv

mkdir -p $TESTDIR

$SLAPPASSWD -g -n >$CONFIGPWF

ITS=9051
ITSDIR=$DATADIR/regressions/its$ITS


echo "Initializing server configuration for provider..."
	DBDIR=${XDIR}/db
	CFDIR=${XDIR}/slapd.d

mkdir -p ${XDIR} $DBDIR.1 $DBDIR.2 $CFDIR
. $CONFFILTER $BACKEND < $ITSDIR/slapd-provider.ldif > $CONFLDIF
$SLAPADD -F $CFDIR -n 0 -l $CONFLDIF
$SLAPADD -F $CFDIR -q -b $BASEDN -l $ITSDIR/db.ldif

KILLPIDS=
CFDIR=${XDIR}/slapd.d

echo "Starting provider slapd on TCP/IP URI $URI1"
$SLAPD -F $CFDIR -h $URI1 -d $LVL > $LOG1 2>&1 &

PID=$!
if test $WAIT != 0 ; then
	echo PID $PID
	read foo
fi
KILLPIDS="$PID $KILLPIDS"
sleep 1

echo "Using ldapsearch to check that provider slapd is running..."
for i in 0 1 2 3 4 5; do
	$LDAPSEARCH -s base -b "" -H $URI1 \
		'objectclass=*' > /dev/null 2>&1
	RC=$?
	if test $RC = 0 ; then
		break
	fi
	echo "Waiting 5 seconds for slapd to start..."
	sleep 5
done

if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Running ldapsearch to generate log ops..."
$LDAPSEARCH -D "$MANAGERDN" -w $PASSWD -s base -b $BASEDN -H $URI1 \
	'objectclass=*' > /dev/null 2>&1
RC=$?

if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Changing to read ops only..."
$LDAPMODIFY -D cn=config -H $URI1 -y $CONFIGPWF > $TESTOUT 2>&1 <<EOF
dn: olcOverlay={0}accesslog,olcDatabase={1}$BACKEND,cn=config
changetype: modify
delete: olcAccessLogOps
olcAccessLogOps: writes

EOF
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed for server config ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Running ldapsearch to generate log ops..."
$LDAPSEARCH -D "$MANAGERDN" -w $PASSWD -s base -b $BASEDN -H $URI1 \
	'objectclass=*' > /dev/null 2>&1
RC=$?

if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Changing to bind, search, compare ops only..."

$LDAPMODIFY -D cn=config -H $URI1 -y $CONFIGPWF >> $TESTOUT 2>&1 <<EOF
dn: olcOverlay={0}accesslog,olcDatabase={1}$BACKEND,cn=config
changetype: modify
replace: olcAccessLogOps
olcAccessLogOps: bind
olcAccessLogOps: compare
olcAccessLogOps: search

EOF

echo "Running ldapsearch to generate log ops..."
$LDAPSEARCH -D "$MANAGERDN" -w $PASSWD -s base -b $BASEDN -H $URI1 \
	'objectclass=*' > /dev/null 2>&1
RC=$?

if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Running ldapcompare to generate log ops..."
$LDAPCOMPARE -D "$MANAGERDN" -w $PASSWD -H $URI1 \
    "uid=user.2,ou=user,$BASEDN" "uidNumber:1006" \
    >> $SEARCHOUT 2>&1
RC=$?
case $RC in
5)
	echo "ldapcompare returned FALSE ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
	;;
6)
	echo "ldapcompare returned TRUE ($RC)"
	;;
0)
	echo "ldapcompare returned success ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit -1
	;;
*)
	echo "ldapcompare failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
	;;
esac
echo "" >> $SEARCHOUT

echo "Exporting accesslog database..."
$SLAPCAT -F $CFDIR -b cn=accesslog -l $TESTDIR/accesslogdb.ldif
RC=$?

test $KILLSERVERS != no && kill -HUP $KILLPIDS

# 1 root entry, 2 binds, 3 searches, 1 compare
count=7
if test $RC != 0 ; then
    echo ">>>>> Test failed"
else
    RC=`grep '^dn:' $TESTDIR/accesslogdb.ldif | wc -l`
    if test $RC != $count ; then
        echo ">>>>> Test failed: expected $count entries, got" $RC
        RC=1
    else
        echo ">>>>> Test succeeded"
        RC=0
    fi
fi

test $KILLSERVERS != no && wait

exit $RC
